//
//  OYHomeController.swift
//  weiboCopycat
//
//  Created by Orange Yu on 2016/11/30.
//  Copyright © 2016年 Orange Yu. All rights reserved.
//

import UIKit
import YYModel

let homeCellReuseIdentifier = "homeCellReuseIdentifier"
class OYHomeController: OYVistorController {

    lazy var homeStatusVM = OYHomeStatusViewModel()
    
     // MARK: -控件懒加载
    lazy var indicatorView: UIActivityIndicatorView = {
        let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
        indicatorView.color = UIColor.darkGray
        return indicatorView
    }()
    
//    lazy var refreshView:UIRefreshControl = {
//        let refreshView = UIRefreshControl()
//        
//        refreshView.addTarget(self, action: #selector(loadNewData), for: .valueChanged)
//        return refreshView
//    }()
    
    /// 设置一个下拉刷新后提醒的label
    private lazy var hoverLabel: UILabel = {
        let label = UILabel()
        label.text = "没有新的微博了"
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.white
        label.backgroundColor = UIColor.orange
        label.textAlignment = .center
        label.frame = CGRect(x: 0, y: self.navigationController!.navigationBar.frame.maxY - 50, width: self.tableView.frame.size.width, height: 50)
        return label
    }()
    
    
    /// 执行hoverLabel动画
    ///
    /// - Parameter hoverMessage: hoverLabel的信息
    func startHoverLabebAni(hoverMessage:String) {
        
        UIView.animate(withDuration: 0.5, animations: {
            self.hoverLabel.transform = CGAffineTransform(translationX: 0, y: 50)
            self.hoverLabel.text = hoverMessage
        }) { (_) in
            UIView.animate(withDuration: 0.5, animations: {
                self.hoverLabel.transform = CGAffineTransform.identity
            }, completion: { (_) in
                
            })
        }
    }
     // MARK: -自定义下拉刷新控件
    lazy var refreshView:OYRefreshControl = {
        
        let refreshView = OYRefreshControl()
        refreshView.addTarget(self, action: #selector(loadNewData), for: .valueChanged)
        return refreshView
        
    }()

    
    
    func loadNewData (){
        loadData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if isLogin == false {
            visitorView?.updateVisitorView(labelTitle: nil, imageName: nil)
        }
        else{
            
            loadData()
            
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.estimatedRowHeight = 200
            
            tableView.register(OYHomeCell.self, forCellReuseIdentifier: homeCellReuseIdentifier)
  
            // 将控件加在footerView上
            tableView.tableFooterView = indicatorView
            tableView.addSubview(refreshView)
            
            // 将下拉刷新提醒label添加在navigationController的view上
            self.navigationController?.view.insertSubview(hoverLabel, belowSubview: (navigationController?.navigationBar)!)
        }
    }
    
    
    /// 请求首页数据
    func loadData() {
        homeStatusVM.loadData (scrollLoding:indicatorView.isAnimating){ (isSuccess,message) in
            
          
            if isSuccess == false {
                print("请求获取首页失败失败")
            } else {

                self.tableView.reloadData()
                if self.indicatorView.isAnimating == false {
                    self.startHoverLabebAni(hoverMessage: message)
                }
                self.indicatorView.stopAnimating()
                // 加载数据成功之后改变refreshControl的状态
                self.refreshView.endRefreshing()
            }
        }
    }

     // MARK: -tableView dataSource
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeStatusVM.homeStatusList.count
    }
   
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: OYHomeCell = tableView.dequeueReusableCell(withIdentifier: homeCellReuseIdentifier, for: indexPath) as! OYHomeCell
        
        let viewModel = homeStatusVM.homeStatusList[indexPath.row]
        
        cell.homeStatusVM = viewModel
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
    }

     // MARK: -tableView delegate
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        // 菊花开始转动
        if indexPath.row == homeStatusVM.homeStatusList.count - 1  && !indicatorView.isAnimating{
            indicatorView.startAnimating()
            // 开始加载数据
            loadData()
        }
        
        
    }
    
}
